<?PHP

$email = $_POST['email'];
$password = $_POST['password'];
for ($i = 0; $i <= 4; $i++) {
    $refused_limited[$i] = $_POST["refused_limited_$i"];
}

$accept_question = '';
$refuse_question = '';
for ($i = 1; $i <= 17; $i++) {
    $question[$i] = $_POST["question_$i"];
    if ($question[$i] == 0) {
        $refuse_question = $refuse_question . "$i,";
        $question_Proposition[$i][0] = '3';
        $question_Proposition[$i][1] = '';
        $question_Opposition[$i] = '';
    } elseif ($question[$i] == 1) {
        $question_Proposition[$i][0] = '4';
        $question_Proposition[$i][1] = '6';
        $question_Opposition[$i] = '5';
        $accept_question = "$i";
    } else {
        $question_Proposition[$i][0] = '';
        $question_Proposition[$i][1] = '';
        $question_Opposition[$i] = '';
    }
}
$refuse_question = rtrim($refuse_question, ",");

$rounds = $_POST['rounds'];
$session = $_POST['session'];
$site_number = $_POST['site_number'];
$chairman_name = $_POST['chairman_name'];
$judge_1_name = $_POST['judge_1_name'];
$judge_2_name = $_POST['judge_2_name'];
$judge_3_name = $_POST['judge_3_name'];
$judge_4_name = $_POST['judge_4_name'];
$judge_5_name = $_POST['judge_5_name'];

$name_1 = $_POST['name_1'];
$school_1 = $_POST['school_1'];
//$chairman_score_1 = $_POST['chairman_score_1'];
for ($judge = 1; $judge <= 5; $judge++) {
    for ($score = 1; $score <= 3; $score++) {
        $judge_score[$judge][$score] = $_POST["judge_" . "$judge" . "_score_" . "$score"];
    }
}
$judge_1_score_1 = $_POST['judge_1_score_1'];
$judge_2_score_1 = $_POST['judge_2_score_1'];
$judge_3_score_1 = $_POST['judge_3_score_1'];
$judge_4_score_1 = $_POST['judge_4_score_1'];
$judge_5_score_1 = $_POST['judge_5_score_1'];

$name_2 = $_POST['name_2'];
$school_2 = $_POST['school_2'];
//$chairman_score_2 = $_POST['chairman_score_2'];
$judge_1_score_2 = $_POST['judge_1_score_2'];
$judge_2_score_2 = $_POST['judge_2_score_2'];
$judge_3_score_2 = $_POST['judge_3_score_2'];
$judge_4_score_2 = $_POST['judge_4_score_2'];
$judge_5_score_2 = $_POST['judge_5_score_2'];

$name_3 = $_POST['name_3'];
$school_3 = $_POST['school_3'];
//$chairman_score_3 = $_POST['chairman_score_3'];
$judge_1_score_3 = $_POST['judge_1_score_3'];
$judge_2_score_3 = $_POST['judge_2_score_3'];
$judge_3_score_3 = $_POST['judge_3_score_3'];
$judge_4_score_3 = $_POST['judge_4_score_3'];
$judge_5_score_3 = $_POST['judge_5_score_3'];








$mysqli = mysqli_init();

$mysqli->options(MYSQLI_OPT_CONNECT_TIMEOUT, 2); //设置超时时间
$mysqli->real_connect('127.0.0.1', 'root', 'mysql');
$sql = "CREATE DATABASE IF NOT EXISTS CUPT DEFAULT CHARSET utf8 COLLATE utf8_general_ci;";
if ($mysqli->query($sql) == TRUE) {
    //printf("Table CUPT successfully created.\n");
}
mysqli_select_db($mysqli, "CUPT");





$question_arr = array
    (
    array(1, 12),
    array(2, 4, 6, 10, 16),
    array(3, 5, 7, 8),
    array(9, 13),
    array(11, 14, 15, 17)
);
$question_number_refuse_arr = array
    (
    array(2, 1),
    array(5, 2),
    array(4, 1),
    array(2, 1),
    array(4, 1)
);
$excess_refused_number = 0;
for ($i = 0; $i <= 4; $i++) {
    if ($refused_limited[$i] < 0) {
        $excess_refused_number = $excess_refused_number - $refused_limited[$i];
    }
}



$participant_score_1 = 3 * (((max($judge_1_score_1, $judge_2_score_1, $judge_3_score_1, $judge_4_score_1, $judge_5_score_1) + min($judge_1_score_1, $judge_2_score_1, $judge_3_score_1, $judge_4_score_1, $judge_5_score_1)) / 2 + array_sum([$judge_1_score_1, $judge_2_score_1, $judge_3_score_1, $judge_4_score_1, $judge_5_score_1]) - (max($judge_1_score_1, $judge_2_score_1, $judge_3_score_1, $judge_4_score_1, $judge_5_score_1) + min($judge_1_score_1, $judge_2_score_1, $judge_3_score_1, $judge_4_score_1, $judge_5_score_1))) / 4 - 0.2 * $excess_refused_number);
$participant_score_2 = 2 * (((max($judge_1_score_2, $judge_2_score_2, $judge_3_score_2, $judge_4_score_2, $judge_5_score_2) + min($judge_1_score_2, $judge_2_score_2, $judge_3_score_2, $judge_4_score_2, $judge_5_score_2)) / 2 + array_sum([$judge_1_score_2, $judge_2_score_2, $judge_3_score_2, $judge_4_score_2, $judge_5_score_2]) - (max($judge_1_score_2, $judge_2_score_2, $judge_3_score_2, $judge_4_score_2, $judge_5_score_2) + min($judge_1_score_2, $judge_2_score_2, $judge_3_score_2, $judge_4_score_2, $judge_5_score_2))) / 4);
$participant_score_3 = 1 * (((max($judge_1_score_3, $judge_2_score_3, $judge_3_score_3, $judge_4_score_3, $judge_5_score_3) + min($judge_1_score_3, $judge_2_score_3, $judge_3_score_3, $judge_4_score_3, $judge_5_score_3)) / 2 + array_sum([$judge_1_score_3, $judge_2_score_3, $judge_3_score_3, $judge_4_score_3, $judge_5_score_3]) - (max($judge_1_score_3, $judge_2_score_3, $judge_3_score_3, $judge_4_score_3, $judge_5_score_3) + min($judge_1_score_3, $judge_2_score_3, $judge_3_score_3, $judge_4_score_3, $judge_5_score_3))) / 4);


$school_arr = ['哈尔滨工业大学（深圳）', '华南理工大学', '华中科技大学', '华中师范大学', '暨南大学', '南方科技大学', '武汉大学', '湘潭大学', '中南大学', '中山大学'];
for ($i = 0; $i <= 9; $i++) {
    $sql = "INSERT INTO school_question_list(school) VALUES('$school_arr[$i]');";
    $mysqli->query($sql);
}


$sql = "select * from volunteer where `email`='$email' and `password`='$password';";
$rst = $mysqli->query($sql);
if ($rst->num_rows < 1) {
    echo '<br />用户名或密码错误！<br />';

    echo '<a href="javascript :;" onClick="javascript :history.back(-1);">回到原界面</a>';
} else {
    //插入各队比赛分数数据
    $sql = "INSERT INTO participant_score_list(name,school,question,rounds,session,participant_score,identity,admin_email) VALUES('$name_1','$school_1','$accept_question','$rounds','$session','$participant_score_1','1','$email');";
    $mysqli->query($sql);
    $sql = "INSERT INTO participant_score_list(name,school,question,rounds,session,participant_score,identity,admin_email) VALUES('$name_2','$school_2','$accept_question','$rounds','$session','$participant_score_2','2','$email');";
    $mysqli->query($sql);
    $sql = "INSERT INTO participant_score_list(name,school,question,rounds,session,participant_score,identity,admin_email) VALUES('$name_3','$school_3','$accept_question','$rounds','$session','$participant_score_3','3','$email');";
    $mysqli->query($sql);


    //插入各队选题拒题情况数据
    for ($i = 1; $i <= 17; $i++) {
        //echo sizeof($question_Proposition[$i]);
        //if (strlen($question_Proposition[$i]) > 0) {
        $sql = "select question_$i from school_question_list where school='$school_1';";
        $rst = $mysqli->query($sql);
        $row = mysqli_fetch_assoc($rst);



        if (!strstr($row["question_$i"], $question_Proposition[$i][0])) {
            $str_tmp = $question_Proposition[$i][0];
            $question_1[$i] = $row["question_$i"] . "$str_tmp";
            $sql = "UPDATE school_question_list SET question_$i='$question_1[$i]' where school='$school_1';";
            $mysqli->query($sql);
        }

        if (!strstr($row["question_$i"], $question_Proposition[$i][1])) {
            $sql = "select question_$i from school_question_list where school='$school_1';";
            $rst = $mysqli->query($sql);
            $row = mysqli_fetch_assoc($rst);


            $str_tmp = $question_Proposition[$i][1];
            $question_1[$i] = $row["question_$i"] . "$str_tmp";
            $sql = "UPDATE school_question_list SET question_$i='$question_1[$i]' where school='$school_1';";
            $mysqli->query($sql);
        }
        //}

        if (strlen($question_Opposition[$i]) > 0) {
            $sql = "select question_$i from school_question_list where school='$school_2';";
            $rst = $mysqli->query($sql);
            $row = mysqli_fetch_assoc($rst);

            if (!strstr($row["question_$i"], $question_Opposition[$i])) {
                $question_2[$i] = $row["question_$i"] . "$question_Opposition[$i]";
                $sql = "UPDATE school_question_list SET question_$i='$question_2[$i]' where school='$school_2';";
                $mysqli->query($sql);
            }
        }
    }





    for ($judge = 1, $string1 = '', $string2 = ''; $judge <= 5; $judge++) {
        for ($score = 1; $score <= 3; $score++) {
            $string1 = $string1 . "judge_" . "$judge" . "_score_" . "$score" . ",";
            $judge_score_tmp = $judge_score[$judge][$score];
            $string2 = $string2 . "$judge_score_tmp,";
        }
    }

    $sql = "INSERT INTO session_match_list(site,round,session,accept_question,refuse_question," . "$string1" . "admin_email) VALUES('$site_number','$rounds','$session','$accept_question','$refuse_question'," . "$string2" . "'$email');";
    $mysqli->query($sql);
    /* $sql = "select * from participant_score_list;";
      $rst = $mysqli->query($sql); */

    //echo '<br />数据录入成功';
    mysqli_close($mysqli);
    header("Location: score-input.html?email=$email&score_input_flag=1");
    exit;
}
?>